package main

import (
	"encoding/json"
	"github.com/gin-contrib/static"
	"github.com/gin-gonic/gin"
	"ys-tongji-go/tool"
	"io/ioutil"
	"net/http"
	"time"
)

func main() {

	r := gin.Default()
	// 初始化路由

	//r.StaticFS("/", http.Dir("dist"))
	//r.Static("/assets", "./dist/assets")
	//r.Static("/icon", "./dist/icon")
	//r.Static("/img", "./dist/img")
	//r.LoadHTMLFiles("dist/index.html")
	//r.GET("/", func(c *gin.Context) {
	//	c.HTML(200, "index.html", nil)
	//})

	r.Use(static.Serve("/", static.LocalFile("./dist", true)))

	r.Use(func(c *gin.Context) {
		method := c.Request.Method
		origin := c.Request.Header.Get("Origin")
		if origin != "" {
			c.Header("Access-Control-Allow-Origin", origin)
			c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, UPDATE")
			c.Header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization")
			c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Cache-Control, Content-Language, Content-Type")
			c.Header("Access-Control-Allow-Credentials", "false")
			c.Set("content-type", "application/json")
		}
		if method == "OPTIONS" {
			c.AbortWithStatus(http.StatusNoContent)
		}
		c.Next()
	})

	r.POST("/api/getUrlById", func(c *gin.Context) {
		cookis, isCookis := c.GetPostForm("cookis")
		uid, isUid := c.GetPostForm("uid")
		if !isCookis {
			c.JSON(200, gin.H{"code": 444, "msg": "你需要输入Cookie"})
			return
		}
		uidList, newCookis, err2 := tool.GetUidList(cookis)
		if err2 != nil {
			if err2.Error() == "" {
				c.JSON(200, gin.H{"code": 444, "msg": "出错了"})
			} else {
				c.JSON(200, gin.H{"code": 444, "msg": err2.Error()})
			}
			return
		}
		if len(uidList) == 0 {
			c.JSON(200, gin.H{"code": 444, "msg": "没有查询到角色"})
			return
		}
		data := tool.UidObj{}
		if len(uidList) == 1 {
			data = uidList[0]
		}
		if len(uidList) > 1 {
			if !isUid {
				c.JSON(200, gin.H{"code": 444, "msg": "需要uid"})
			}
			b := false
			for _, obj := range uidList {
				if obj.GameUid == uid {
					data = obj
					b = true
				}
			}
			if !b {
				c.JSON(200, gin.H{"code": 444, "msg": "你有多个uid但没有找到你输入的，请检查"})
				return
			}
		}
		url, err2 := tool.GetJsonUrl(data.GameUid, data.GameBiz, data.Region, newCookis)

		c.JSON(200, gin.H{"code": 200, "msg": "成功", "data": url})

	})
	//（"新手祈愿"，100），（"常驻祈愿"， 200），（"活动祈愿1&2"，301 )，（"武器祈愿"，302）
	r.POST("/api/getListByEnd", func(c *gin.Context) {
		url, isUrl := c.GetPostForm("url")
		end_id, isEndId := c.GetPostForm("end_id")
		gacha_type, isGachaType := c.GetPostForm("gacha_type")
		if !isUrl || !isGachaType {
			c.JSON(200, gin.H{"code": 444, "msg": "参数不完整"})
			return
		}
		if isEndId {
			url += end_id
		}
		url += "&gacha_type=" + gacha_type
		resp, err2 := http.Get(url)
		if err2 != nil {
			c.JSON(200, gin.H{"code": 444, "msg": "出错了"})
			return
		}

		type item struct {
			Uid       string `json:"uid"`
			GachaType string `json:"gacha_type"`
			ItemId    string `json:"item_id"`
			Count     string `json:"count"`
			Time      string `json:"time"`
			Name      string `json:"name"`
			Lang      string `json:"lang"`
			ItemType  string `json:"item_type"`
			RankType  string `json:"rank_type"`
			Id        string `json:"id"`
		}
		type dataObj struct {
			List []item `json:"list"`
		}
		type ReqAuthData struct {
			Data dataObj `json:"data"`
		}

		body2, _ := ioutil.ReadAll(resp.Body)
		authData := ReqAuthData{}
		json.Unmarshal(body2, &authData)

		c.JSON(200, gin.H{"code": 200, "msg": "成功", "data": authData.Data.List})
	})

	r.GET("PING", func(c *gin.Context) {
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "PING",
			"data": time.Now().UnixNano() / 1e6,
		})
	})

	r.Run(":3032")
}
